package com.heima.leadnews.article.listener;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.leadnews.article.service.ApArticleConfigService;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.wemedia.dto.WmNewsDownOrUpDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class WmNewsDownOrUpListener {

    @Autowired
    private ApArticleConfigService configService;

    @KafkaListener(topics = {WmNewsDownOrUpDTO.NEWS_DOWN_UP})
    public void receiveMessage(ConsumerRecord<String,String> record){
        //文章的id
        Long articleId = Long.valueOf(record.key());
        //文章的状态
        Boolean isEnable = Boolean.valueOf(record.value());
        //设置修改条件，修改用户文章配置表
        LambdaQueryWrapper<ApArticleConfig> updateWrapper = new LambdaQueryWrapper<>();
        updateWrapper.eq(ApArticleConfig::getArticleId,articleId);
        updateWrapper.eq(ApArticleConfig::getIsDown,!isEnable);
        configService.update(updateWrapper);
    }
}
